<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	  xmlns:th="http://www.thymeleaf.org"
	  lang="zh-cn">
<head>
	<meta content="云之讯,云通讯,云通讯平台" name="keywords"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
	<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
	<title>短信云平台运营系统 - 资源管理</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
	<link th:href="@{/resources/css/bootstrap.min14ed.css?v=3.3.6}" rel="stylesheet">
	<link th:href="@{/resources/css/font-awesome.min93e3.css?v=4.4.0}" rel="stylesheet"><!-- jqgrid-->
	<link th:href="@{/resources/js/plugins/jqgrid/css/ui.jqgrid-bootstrap.css}" rel="stylesheet">
	<link th:href="@{/resources/css/animate.min.css}" rel="stylesheet">
	<link th:href="@{/resources/css/style.min862f.css?v=4.1.0}" rel="stylesheet">
	<link th:href="@{/resources/css/plugins/sweetalert/sweetalert.css}" rel="stylesheet">
	<style>
		/* Additional style to fix warning dialog position */
		#alertmod_table_list_2 {
			top: 900px !important;
		}
	</style>
</head>
<body class="gray-bg"><div th:replace="stat :: statCode"></div>
<div class="wrapper wrapper-content  animated fadeInRight">
	<div class="row">
		<div class="col-sm-12">
			<div class="tabs-container">
				<ul class="nav nav-tabs">
					<li class="active" onclick="doResize(1)" th:if="${menus.wtjd261}"><a data-toggle="tab" href="#tab-1" aria-expanded="true"> 我提交的</a>
					</li>
					<!-- <li class="" onclick="doResize(2)"><a data-toggle="tab" href="#tab-2" aria-expanded="false"> 我审批的</a>
					</li> -->
					<li class="" onclick="doResize(3)" th:if="${menus.zyxq}"><a data-toggle="tab" href="#tab-3" id="tab_3" aria-expanded="false"> 资源需求</a>
					</li>
				</ul>
				<div class="tab-content">
					<!-- 我提交的 -->
					<div id="tab-1" class="tab-pane active" th:if="${menus.wtjd261}">
						<div class="panel-body">
							<div class="row">
								<div class="ibox ">
									<!-- 搜索条件 -->
									<div class="ibox-content">
										<div class="col-sm-12">
											<form class="form-inline" method="post" id="mainForm_1" >
												<div class="form-group pull-right m-r-md">
													<span>&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-danger" onclick="search(1)">搜索</button></span>
													<span>&nbsp;&nbsp;<a type="button" class="btn btn-sm btn-primary" th:href="@{/order/resource/add}">提交资源</a></span>
												</div>
	
												<div class="form-group pull-right m-r-md">
													<label for="state_1">资源状态:</label>
													<select id="state_1" name="state" class="form-control">
														<option value="">全部</option>
														<option value="0">待接入</option>
														<option value="1">已接入</option>
														<option value="2">待审批</option>
														<option value="3">撤销</option>
													</select>
												</div>
	
											</form>
										</div>
									</div>
								</div>
							</div>

							<div class="ibox-content">
								<div class="jqGrid_wrapper">
									<table id="table_list_1"></table>
									<div id="pager_list_1"></div>
								</div>
							</div>
						</div>
					</div>
					<!-- 资源需求 -->
					<div id="tab-3" class="tab-pane" th:if="${menus.zyxq}">
						<div class="panel-body">
							<div class="ibox ">
							</div>

							<div class="ibox-content">
								<div class="jqGrid_wrapper">
									<table id="table_list_3"></table>
									<div id="pager_list_3"></div>
								</div>
							</div>
						</div>
					</div>

				</div>
			</div>
		</div>
	</div>
</div>
<div class="modal fade" id="wuziyuanModal" demandId tabindex="-1" role="dialog" aria-labelledby="exampleModalLabe3">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="exampleModalLabel">无资源</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <textarea class="form-control" id="remark-wuziyuan" placeholder="填写备注(50字符以内）"></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="wuziyuan(this)">确定</button>
<!--                 <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> -->
            </div>
        </div>
    </div>
</div>

<script th:src="@{/resources/js/jquery.min.js?v=2.1.4}"></script>
<script th:src="@{/resources/js/bootstrap.min.js?v=3.3.6}"></script>
<script th:src="@{/resources/js/content.min.js?v=1.0.0}"></script>
<script th:src="@{/resources/js/layer/layer.js}"></script>
<script th:src="@{/resources/js/plugins/layer/laydate/laydate.js}"></script>
<script th:src="@{/resources/js/plugins/peity/jquery.peity.min.js}"></script>
<script th:src="@{/resources/js/plugins/jqgrid/js/i18n/grid.locale-cn.js}"></script>
<script th:src="@{/resources/js/plugins/jqgrid/js/jquery.jqGrid.min.js}"></script>
<script th:src="@{/resources/js/date_format.min.js?v=1.0.0}"></script>
<script th:src="@{/resources/js/plugins/sweetalert/sweetalert.min.js}"></script>
<script th:src="@{/resources/js/common.js?v=1.0.0}"></script>

<script th:inline="javascript">
    var max_export_num = [[${max_export_num}]];

    var grid_width = 1200; // 默认宽度
    var menus = [[${menus}]];
    if (menus.wtjd261) {

    } else if (menus.zyxq) {
        $("#tab_3")[0].click();
    }
    function doSearch(oper){
		if ($("#tab-1").hasClass("active")){
            search(1);
		} else if ($("#tab-3").hasClass("active")){
            search(3);
		} else {
        }
	}
    
    function search(type){
        if(type == 1){
            $("#table_list_" + type).jqGrid('setGridParam',{
                datatype:'json',
                postData: {
                    state: $("#state_"+type).val()
                }, //发送数据
            }).trigger("reloadGrid"); //重新载入
		}else if(type == 3){
			$("#table_list_" + type).jqGrid('setGridParam',{
                datatype:'json'
//                 postData: {
//                     state: $("#state_"+type).val()
//                 }, //发送数据
            }).trigger("reloadGrid"); //重新载入
		}
    }

    function doResize(type){
        var width = $(".jqGrid_wrapper").width();
        if(width > 0){
			grid_width = width;
        }
        $("#table_list_"+ type).setGridWidth(grid_width);
    }

    $(document).ready(function () {
        $.jgrid.defaults.styleUI = "Bootstrap";
        
        // 如果是从“选择资源”页面过来的需要将tab页签切换到“资源需求”
        if(getUrlParam("fromPage") == "demand-hasRes"){
        	$(".nav-tabs").find("li").eq(1).find("a").trigger("click");
        }

        $("#table_list_1").jqGrid({
            url:/*[[@{/order/resource/list}]]*/,
			mtype: 'GET',
			datatype: "json",
			jsonReader : {
				root:"data.entityList",
				page: "data.currentPage",
				total: "data.totalPage",
				records: "data.totalCount"
			},
			height: 'auto',
			rownumbers:true,
			autowidth: true,
			shrinkToFit: false,
			autoScroll: true,
			rowNum: 20,
			rowList: [10, 20, 30,50],
			colNames: ["资源编号","通道号","通道类型","发送类型","发送内容属性","是否直连","是否保底", "是否授信", 
			           "归属商务", "扩展位", "速率", "签名", "结算方式", "采购价格（元/条）", "待接入时间","状态","操作"],
			colModel: [
			    // 资源编号
				{name: "resourceId",align: "left",sortable:false, width:80},
				// 通道号
				{name: "channelId",align: "left",sortable:false, width:80},
				// 通道类型
				{name: "channelType",align: "left",sortable:false, width:80,
					formatter:function (cellvalue) {
                        var arr = cellvalue.split(",");
                        var channelType = '';
                        for(var i in arr){
                            switch (arr[i]){
                                case '0':
                                    channelType += channelType.length > 0 ? ' , 三网合一' :'三网合一';
                                    break;
                                case '1':
                                    channelType += channelType.length > 0 ? ' , 移动':'移动';
                                    break;
                                case '2':
                                    channelType += channelType.length > 0 ? ' , 电信':'电信';
                                    break;
                                case '3':
                                    channelType += channelType.length > 0 ? ' , 联通':'联通';
                                    break;
                                case '4':
                                    channelType += channelType.length > 0 ? ' , 全网':'全网';
                                    break;
                            }
                        }
                        return channelType;
                    }
				},
				// 发送类型
				{name: "smsType",align: "left",sortable:false, width:80,
					formatter:function(cellvalue){
						var reg = new RegExp("\\$\\#","g"); 
                        return cellvalue.replace(reg, "，");
                    }
				},
				// 发送内容属性
				{name: "contentProp",align: "left",sortable:false,width:100},
				// 是否直连
				{name: "directConnect",align: "left",sortable:false,width:'65',formatter:"select",editoptions:{value:"0:直连;1:第三方"}},
				// 是否保底
				{name: "minimumGuarantee",align: "left",sortable:false,width:100,
					formatter:function(cellvalue){
                        if(/^[1-9]\d*$/.test(cellvalue)){
                            cellvalue += '万条/月';
                        }else{
                            cellvalue = '不承诺保底';
                        }
                        return cellvalue;
                    }
				},
				// 是否授信
            	{name: "isCredit",align: "left",sortable:false, width:100,
					formatter:function(cellvalue){
                        if(/^[1-9]\d*$/.test(cellvalue)){
                            cellvalue += '万条/月';
                        }else{
                            cellvalue = '不授信';
                        }
                        return cellvalue;
                    }
            	},
            	// 归属商务
            	{name: "belongBusinessName",align: "left",sortable:false,width:100},
				// 扩展位
				{name: "extendSize",align: "left",sortable:false,width:60},
				// 速率
            	{name: "rate",align: "left",sortable:false, width:100,
					formatter:function(cellvalue){
                        if(/^\d+$/.test(cellvalue)){
                            cellvalue += ' TPS';
                        }else{
                            cellvalue = '-';
                        }
                        return cellvalue;
                    }
            	},
            	// 签名
            	{name: "signType",align: "left",sortable:false, width:80,
            		formatter:function (cellvalue) {
                        var arr = cellvalue.split(",");
                        var signType = '';
                        for(var i in arr){
                            switch (arr[i]){
                                case '0':
                                    signType += signType.length > 0 ? ' , 自定义' :'自定义';
                                    break;
                                case '1':
                                    signType += signType.length > 0 ? ' , 报备签名':'报备签名';
                                    break;
                                case '2':
                                    signType += signType.length > 0 ? ' , 固签':'固签';
                                    break;
                            }
                        }
                        return signType;
                    }
            	},
            	// 结算方式
            	{name: "payType",align: "left",sortable:false,width:'100'},
            	// 采购价格（元/条）
            	{name: "purchasePrice",align: "left",sortable:false,width:'120'},
            	// 待接入时间
            	{name: "onlineDate",align: "left",sortable:false,width:'100'},
            	// 状态
            	{name: "state",align: "left",sortable:false,width:'65',formatter:"select",editoptions:{value:"0:待接入;1:已接入;2:待审批;3:撤销"}},
            	// 操作
            	{name: "total",index: "total",align: "left",sortable:false,title:false,
            		formatter:function(cellvalue, options, rowObject){
                        var status = rowObject.state;
                        var resourceId = rowObject.resourceId;
                        var switcher = "<button type='button' class='btn btn-link btn-xs ' onclick=\"getDetails(1, '"+resourceId+"')\" title='查看'>查看</button>";
                        if(/^[0]$/.test(status)){//编辑(3)
                            switcher += "<button type='button' class='btn btn-link btn-xs ' onclick=\"edit('"+resourceId+"')\" title='编辑'>编辑</button>";
                        }
                        if(/^[0,1,2]$/.test(status)){ // “待接入”、“已接入”、“待审批”状态下可以“撤销”
                            switcher += "<button type='button' class='btn btn-link btn-xs ' onclick=\"updateState(this,'"+resourceId+"')\" title='撤销'>撤销</button>";
                        }
                        return switcher;
                    }
            	}
			],
            pager: "#pager_list_1",
            viewrecords: true,
            hidegrid: false
                   
    	});

        $("#table_list_3").jqGrid({
           	url:/*[[@{/order/resource/demand/list}]]*/,
            mtype: 'GET',
			datatype: "json",
			jsonReader : {
				root:"data.entityList",
				page: "data.currentPage",
				total: "data.totalPage",
				records: "data.totalCount"
			},
			height: 'auto',
            rownumbers:true,
            autowidth: true,
            shrinkToFit: false,
            autoScroll: true,
            rowNum: 20,
            rowList: [10, 20, 30,50],
            colModel: [
				{ label: "需求编号",name: "demandId",align: "left",sortable:false, width:120},
				{ label:"所属行业",name: "industryType",align: "left",sortable:false, width:80},
				{ label:"预计量（条/月）",name: "expectNumber",align: "left",sortable:false, width:100},
				{ label:"是否保底",name: "minimumGuarantee",align: "left",sortable:false,width:85,
					formatter:function(cellvalue){
                        if(/^[1-9]\d*$/.test(cellvalue)){
                            cellvalue += '万条/月';
                        }else{
                            cellvalue = '不承诺保底';
                        }
                        return cellvalue;
                    }
				},
				{ label:"发送类型",name: "smsType",align: "left",sortable:false,width:100},
				{ label:"通道类型",name: "channelType",align: "left",sortable:false,width:100,
					formatter:function (cellvalue) {
                        var arr = cellvalue.split(",");
                        var channelType = '';
                        for(var i in arr){
                            switch (arr[i]){
                                case '0':
                                    channelType += channelType.length > 0 ? ' , 三网合一' :'三网合一';
                                    break;
                                case '1':
                                    channelType += channelType.length > 0 ? ' , 移动':'移动';
                                    break;
                                case '2':
                                    channelType += channelType.length > 0 ? ' , 电信':'电信';
                                    break;
                                case '3':
                                    channelType += channelType.length > 0 ? ' , 联通':'联通';
                                    break;
                                case '4':
                                    channelType += channelType.length > 0 ? ' , 全网':'全网';
                                    break;
                            }
                        }
                        return channelType;
                    }
				},
				{ label:"是否直连",name: "directConnect",align: "left",sortable:false,width:80,formatter:"select",editoptions:{value:"0:直连;1:第三方"}},
				{ label:"扩展位",name: "extendSize",align: "left",sortable:false,width:60},
				{ label:"速率",name: "rate",align: "left",sortable:false,width:80,
					formatter:function(cellvalue){
                        if(/^\d+$/.test(cellvalue)){
                            cellvalue += ' TPS';
                        }else{
                            cellvalue = '-';
                        }
                        return cellvalue;
                    }
				},
				{ label:"签名",name: "signType",align: "left",sortable:false,width:100,
					formatter:function (cellvalue) {
                        var arr = cellvalue.split(",");
                        var signType = '';
                        for(var i in arr){
                            switch (arr[i]){
                                case '0':
                                    signType += signType.length > 0 ? ' , 自定义' :'自定义';
                                    break;
                                case '1':
                                    signType += signType.length > 0 ? ' , 报备签名':'报备签名';
                                    break;
                                case '2':
                                    signType += signType.length > 0 ? ' , 固签':'固签';
                                    break;
                            }
                        }
                        return signType;
                    }
				},
				{ label:"发送内容模板",name: "contentTemplate",align: "left",sortable:false},
				{ label:"销售价格（元/条）",name: "salePrice",align: "left",width:90,sortable:false},
				{ label:"要求上线时间",name: "onlineDate",align: "left",width:90,sortable:false},
				{ label:"需求状态",name: "state",align: "left",width:80,sortable:false,
					formatter:function (cellvalue) {
                        var arr = cellvalue.split(",");
                        var signType = '';
                        for(var i in arr){
                            switch (arr[i]){
                                case '0':
                                    signType = '寻资源';
                                    break;
                                case '1':
                                    signType = '已供应';
                                    break;
                                case '2':
                                    signType = '无资源';
                                    break;
                                case '3':
                                    signType = '撤单';
                                    break;
                            }
                        }
                        return signType;
                    }
				},
				{ label:"备注",name: "remark",align: "left",sortable:false},
				{ label:"操作",name: "total",index: "total",align: "left",sortable:false,
					formatter:function(cellvalue, options, rowObject){
						var demandId = rowObject.demandId;
						var state = rowObject.state;
						
						var switcher = "";
						
                        if(/^[0]$/.test(state)){// 寻资源
                            switcher += "<button type='button' class='btn btn-link btn-xs ' onclick=\"youziyuan('"+demandId+"')\">有资源</button>";
                            switcher += "<button type='button' class='btn btn-link btn-xs ' data-toggle='modal' data-target='#wuziyuanModal' onclick=\"setDemandId2Modal('"+demandId+"')\">无资源</button>";
                        }
                        if(/^[1,2,3]$/.test(state)){ // “已供应”、“无资源”、“撤单”状态下可以“查看”
                            switcher += "<button type='button' class='btn btn-link btn-xs ' onclick=\"getDetails(3,'"+demandId+"')\">查看</button>";
                        }
                        return switcher;
					}
				}
            ],
            pager: "#pager_list_3",
            viewrecords: true,
            hidegrid: false
    	});

        $(window).bind("resize", function () {
            var width = $(".jqGrid_wrapper").width();

            $("#table_list_1").setGridWidth(width);
        })

        function keyUp(e) {
            var currKey=0,e=e||event;
            currKey=e.keyCode||e.which||e.charCode;
            var keyName = String.fromCharCode(currKey);
            if (currKey == 13){
                doSearch();
            }
        }
        document.onkeyup = keyUp;
        
    });
    
 	//修改状态
    function updateState(a, resourceId) {
        layer.confirm("确定要" + $(a).text() + "吗？", function(index){
            //do something
            $.ajax({
                url:/*[[@{/order/resource/repeal}]]*/'',
                type:"POST",
                data:{
                	resourceId : resourceId
                },
                success:function(data){
                    if(data == null){
                        layer.msg('系统错误，请稍后再试', {icon: 2,time: 1500});
                    }else if(data.code == 0){
                        layer.msg(data.msg, {icon: 1,time: 1500});
                    }else{
                        layer.msg(data.msg, {icon: 2,time: 1500});
                    }
                    search(1);
                }
            });
            layer.close(index);
        });
    }
 	
  	//编辑
 	function edit(resourceId){
 		var editUrl = /*[[@{/order/resource/edit?resourceId=}]]*/;
        location.href = editUrl + resourceId;
 	}
  	
  	function getDetails(type, targetId){
		var detailUrl;
		var boxSize = ['600px', '600px'];
  		if(type == 1){
  			// 查看资源
			detailUrl = /*[[@{/order/resource/detail?resourceId=}]]*/;
  		}else if(type == 3){
  			// 查看资源需求
			detailUrl = /*[[@{/order/resource/demand/detail?demandId=}]]*/;
			boxSize = ['1200px', '600px'];
  		}
  		
		layer.open({
		    type: 2,
		    title: ['查看详情', 'font-size:18px;'],
		    closeBtn: 2,
		    fix: false, //不固定
		    area: boxSize,
		    content: detailUrl + targetId
		});
		
  	}
  	
  	// 打开“无资源”摸太框的时候将对应表单记录的demandId获得
  	function setDemandId2Modal(demandId){
  		$("#remark-wuziyuan").val("");
  		$("#wuziyuanModal").attr("demandId", demandId);
  	}
  	
  	// “无资源”操作
  	function wuziyuan(obj){
  		if($(obj).hasClass("disabled")){
			return;
		}
  		
  		var demandId = $("#wuziyuanModal").attr("demandId");
  		var remark = $("#remark-wuziyuan").val();
  		if($.trim(remark).length > 50){
  			 layer.msg("备注不能超过50个字", {icon: 1,time: 2000});
  			 return;
  		}
  		$(obj).addClass("disabled");
  		
  	
		$.ajax({
	        url:/*[[@{/order/resource/demand/nores}]]*/,
	        type:"POST",
	        data:{
	        	demandId : demandId,
	        	remark : remark
	        },
	        success:function(data){
	            if(data == null){
	                layer.msg('系统错误，请稍后再试', {icon: 2,time: 1500});
	            }else if(data.code == 0){
	                search(3);
	                layer.msg(data.msg, {icon: 1,time: 2000});
	            }else{
	            	layer.alert(data.msg, function(index){
		                search(3);
		                layer.close(index);
	            	});
	            }
	            $(obj).removeClass("disabled");
	        }
		});
		
  		$('#wuziyuanModal').modal('hide');
  	}
  	
  	// “有资源”操作
  	function youziyuan(demandId){
  		var url = /*[[@{/order/resource/demand/hasres?demandId=}]]*/;
        location.href = url + demandId;
  	}
  	
  	function getUrlParam(name) {
  		 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  		 var r = window.location.search.substr(1).match(reg); //匹配目标参数
  		 if (r != null) return unescape(r[2]); return null; //返回参数值
	}
</script>

</body>
</html>